package Dao;

import beans.Staff;

import java.sql.*;
import java.util.Dictionary;

public class LoginDao {
    private final static String URL = "jdbc:sqlserver://localhost:1433;databasename=Travel;encrypt=true;trustServerCertificate=true";
    private final static String UNAME = "sa";
    private final static String PWD = "123";
    public static int login(Staff staff){//1:登录成功;0:登录失败;-1:系统故障
        Connection connection = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        int result  =-1;//登录状态
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            connection = DriverManager.getConnection(URL,UNAME,PWD);
            String sql = "select count(*) from staff where sno = ? and upwd = ? and sposition = ?";
            pstmt = connection.prepareStatement(sql);
            pstmt.setString(1,staff.getSno());
            pstmt.setString(2,staff.getUpwd());
            pstmt.setString(3, staff.getPosition());
            rs = pstmt.executeQuery();
            if(rs.next()){
            	result = rs.getInt(1);
            }
            if(result>0){
                return 1;
            }else {
                return 0;
            }


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return  -1;
        }catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }finally {
            try {
                if(rs!=null) rs.close();
                if(pstmt!=null) pstmt.close();
                if(connection!=null) connection.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }
}
